package com.dogkr.server.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dogkr.common.Constants;
import com.dogkr.server.entity.District;
import com.dogkr.server.entity.DistrictArea;
import com.dogkr.server.mapper.DistrictAreaMapper;
import com.dogkr.server.mapper.DistrictMapper;
import com.sean.common.service.BaseService;

@Service
public class DistrictService extends BaseService {
	@Autowired
	private DistrictMapper districtMapper;
	@Autowired
	private DistrictAreaMapper districtAreaMapper;
	
	public void insertDistrictWithAll(District district) {
		district.setCreateDate(new Date());
		district.setLastModifydate(district.getCreateDate());
		district.setStatus(Constants.STATUS_NORMAL);
		districtMapper.insert(district);
		if(district.getDistrictAreaList() != null) {
			double orderNo = 1d;
			for(DistrictArea districtArea : district.getDistrictAreaList()) {
				districtArea.setDisId(district.getId());
				districtArea.setCreateDate(new Date());
				districtArea.setLastModifydate(districtArea.getCreateDate());
				districtArea.setStatus(Constants.STATUS_NORMAL);
				districtArea.setOrderNo(orderNo);
				orderNo += 1;
				districtAreaMapper.insert(districtArea);
			}
		}
	}
	
	public List<District> selectDistrictByLoaclId(Long localId) {
		return districtMapper.selectDistrict(this.getParamMap("localId", localId));
	}

	public List<DistrictArea> selectDistrictAreaByDisId(Long disId) {
		return districtAreaMapper.selectByDisKey(disId);
	}
}
